﻿using System;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
using ToDoListReminder.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text;
namespace UintTest
{
    
    
    /// <summary>
    ///This is a test class for ProjectTest and is intended
    ///to contain all ProjectTest Unit Tests
    ///</summary>
	[TestClass]
	public class ProjectTest
	{
		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get;
			set;
		}

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion


		/// <summary>
		///A test for Project serialization.
		///</summary>
		[TestMethod]
		public void ProjectSerializationTest()
		{
			// Create source object.
			var sourceObject = new Project(@"c:\work\test.tdi");
			Assert.IsNotNull(sourceObject, "Failed create a new instance.");

			// Create writer
			var buffer = new StringBuilder();
			var xmlWriter = XmlWriter.Create(buffer);
			Assert.IsNotNull(xmlWriter, "Failed create strung buffered writer");

			// Create serializer.
			var serializer = new DataContractSerializer(typeof(Project));

			//Serialize.
			serializer.WriteObject(xmlWriter, sourceObject);
			xmlWriter.Flush();

			// Create reader
			var stringReader = new StringReader(buffer.ToString());
			var xmlReader = XmlReader.Create(stringReader);

			// Deserialize
			var deserializedObject = (Project)serializer.ReadObject(xmlReader);
			Assert.IsTrue(sourceObject.ToString() == deserializedObject.ToString(), "Failed serialization/re-serialization");
		}

		/// <summary>
		/// A test for save and restore.
		/// </summary>
		[TestMethod]
		public void SaveRestoreTest()
		{
			// Source data members.
			const string fileName = @"c:\work\test.tdi";

			// Create object.
			var sourceObject = new Project(fileName);

			// Begin edit mode.
			sourceObject.BeginEdit();

			// Change property.
			sourceObject.FileName = "hhhhh";

			// Cancel changes.
			sourceObject.CancelEdit();

			// Check.
			Assert.AreEqual<String>(fileName, sourceObject.FileName, "Failed save/restore");
		}
	}
}
